#include "floatingdialog.h"
#include "ui_floatingdialog.h"
#include <math.h>
#include <QTextStream>

FloatingDialog::FloatingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FloatingDialog)
{
    ui->setupUi(this);

}

FloatingDialog::~FloatingDialog()
{
    delete ui;
}

void FloatingDialog::slotNewPixel(QPointF pos)
{
    double x=pos.x();
    double y=pos.y();

    ui->leMouseX->setText(QString::number(x));
    ui->leMouseY->setText(QString::number(y));
}

void FloatingDialog::slotStar(int x, int y)
{
    QString star;
    star = star.sprintf("%d\t%d", x, y);
    ui->listWidget->addItem(star);
}

void FloatingDialog::slotStarCount(int count)
{
    QString msg;
    msg = msg.sprintf("# stars found = %d", count);
    ui->leNumStars->setText(msg);

    update();
    qApp->processEvents();
}
void FloatingDialog::slotStatus(QString stat)
{
    ui->leStatus->setText(stat);

    update();
    qApp->processEvents();
}

void FloatingDialog::slotNewRGB(int x, int y, QRgb pixel)
{
    QString item;
    double r=qRed(pixel);
    double g=qGreen(pixel);
    double b=qBlue(pixel);
    double intensity=sqrt(r*r+g*g+b*b);
    item.sprintf("%lf", intensity  );
    ui->tableWidget->setItem(x, y, new QTableWidgetItem(item));
}

void FloatingDialog::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    int x, y;
    QString star = current->text();
    QTextStream(&star) >> x >> y;
    emit sigCenterOn(x,y);
}

void FloatingDialog::on_listWidget_currentTextChanged(const QString &currentText)
{

}
